// index.js
import request from "../../utils/request"
import { url } from "../../utils/util"
import { config,constVAR } from "../../config"
import { store } from "../../utils/store"
Page({
  data: {
    isLoading: false,
    cover: "../../assets/cover.jpg",
  },
  onLoad(options) {
    const timer = setInterval(res => {
      let data = getApp().globalData.user
      if(data != undefined && data.openid.length > 0) {
        clearInterval(timer)
        this.setData({
          cover: url(data.cover)
        })
      }
    }, 500)
  },
  startChat(e) {
    let data = getApp().globalData.user
    if(data.nickName && data.openid) {
      wx.navigateTo({
        url: '/pages/chat/chat',
      })
      return
    }
    this.setData({
      isLoading: true
    })
    wx.getUserProfile({
      desc: '展示用户个性内容',
    }).then(res => {
      data.avatar = res.userInfo.avatarUrl
      data.name =  res.userInfo.nickName
      store.put(constVAR.KEY_APP, JSON.stringify(data))
      wx.navigateTo({
        url: '/pages/chat/chat',
      })
      wx.nextTick(()=> {
        this.setData({
          isLoading: false
        })
      })
    })
  },
})
